package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.YwcgEnumUtil;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.linlinjava.litemall.db.domain.BusinessResult;
import org.linlinjava.litemall.db.service.BusinessProjectService;
import org.linlinjava.litemall.db.service.BusinessResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static org.linlinjava.litemall.db.domain.BusinessResult.Column.backupRemark;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/4 9:50
 * @Version: 1.0
 * @Description:
 */
@Service
public class AdBusinessAuditService {

    @Autowired
    private BusinessProjectService businessProjectService;


    @Autowired
    private BusinessResultService businessResultService;


    @Autowired
    private LogHelper logHelper;


    // 判断项目状态是否可编辑和删除，审核中和审核通过的项目不能编辑和删除
    public boolean canEdit(String projectId, int code) {
        // 如果是科研业务项目模块查询科研业务项目
        if (YwcgEnumUtil.ModelNameCode.KYP.getCode() == code){
            BusinessProject businessProject = businessProjectService.selectByPrimaryKey(projectId);
            // 判断项目状态,如果是审核中和审核通过的状态为不可编辑
            if (YwcgEnumUtil.StatusAuditCode.waitPass == businessProject.getStatusAudit() || YwcgEnumUtil.StatusAuditCode.pass == businessProject.getStatusAudit()){
                return false;
            }
        }else {
            BusinessResult businessResult = businessResultService.selectByPrimaryKey(projectId);
            // 判断项目状态,如果是审核中和审核通过的状态为不可编辑
            if (YwcgEnumUtil.StatusAuditCode.waitPass == businessResult.getStatusAudit() || YwcgEnumUtil.StatusAuditCode.pass == businessResult.getStatusAudit()){
                return false;
            }
        }
        return true;
    }


    // 修改项目审核状态
    public Object updateStatusAudit(String projectId, Integer modelId,Integer statusAudit,String backupRemark) {
        // 如果是项目审批，进入项目审批
        if (YwcgEnumUtil.ModelNameCode.KYP.getCode() == modelId){
            BusinessProject businessProject = new BusinessProject();
            businessProject.setProjectId(projectId);
            businessProject.setStatusAudit(statusAudit);
            if (!StringUtils.isEmpty(backupRemark)){
                businessProject.setBackupRemark(backupRemark);
            }
            // 待审核状态
            businessProjectService.updateByPrimaryKeySelective(businessProject);
        }else { // 否则就是成果库中的信息
            BusinessResult businessResult = new BusinessResult();
            businessResult.setResId(projectId);
            businessResult.setStatusAudit(statusAudit);
            if (!StringUtils.isEmpty(backupRemark)){
                businessResult.setBackupRemark(backupRemark);
            }
            businessResultService.updateByPrimaryKeySelective(businessResult);
        }
        return ResponseUtil.ok();
    }

    // 查询项目审核状态
    public Integer selectAuditStatusByPrimaryKey(String projectId, Integer modelId) {
        // 如果是项目审批，进入项目审批
        if (YwcgEnumUtil.ModelNameCode.KYP.getCode() == modelId){

            // 待审核状态
            BusinessProject businessProject = businessProjectService.selectById(projectId);
            if (businessProject == null){
                return -1;
            }
            return businessProject.getStatusAudit();
        }else { // 否则就是成果库中的信息
            BusinessResult businessResult = businessResultService.selectByPrimaryKey(projectId);
            if (businessResult == null){
                return -1;
            }
            return businessResult.getStatusAudit();
        }
    }
}
